<?
	if($_SESSION['is_login'] != true){
		header("Location: ".$root."/dang-nhap/");
	}
	
	if(isset($_GET['pid'])){
		$pid = intval($_GET['pid']);
	}else{
		reHome($urlHome);
	}
	
	$_SESSION['url'] = $urlHome;
	
	if(isset($_GET['act']) && ($_GET['act'] == 'xoa')){
		$db->query("UPDATE ".$prefix."sanpham SET t_hinhanh = '' WHERE t_id = '".$pid."'");
		header("Location: ".$root."/sua-san-pham/".$pid."");
	}
	
	if(isset($_POST['submit'])){
		$pid = $_POST['pid'];
		$t_tieude = addslashes($_POST['t_tieude']);
		$t_mota = addslashes($_POST['t_mota']);
		$t_loaisanpham = $_POST['t_loaisanpham'];
		$t_nguoidang = $_SESSION['uid'];
		//Hinhanh
		$DuongDanAnh_tmp = $upload->upload_file($PathFile,'t_hinhanh');
		
		if($DuongDanAnh_tmp != ''){
			$filetype = $upload->file_type($DuongDanAnh_tmp);
			$upload->crop_image($imgWidth,$imgHeight,$PathFile . $DuongDanAnh_tmp,$filetype,$PathThumbFile .$DuongDanAnh_tmp);
			$DuongDanAnh = "'" . $DuongDanAnh_tmp . "'";
		}else{
			$DuongDanAnh = "`t_hinhanh`";
		}	
		
		//
		$t_giaban = addslashes($_POST['t_giaban']);
		$t_loaitien = $_POST['t_loaitien'];
		$t_giathitruong = addslashes($_POST['t_giathitruong']);
		$t_loaitien_giathitruong = $_POST['t_loaitien_giathitruong'];
		$txtQuocGia = $_POST['txtQuocGia'];
		$txtTinhThanh = $_POST['txtTinhThanh'];
		$chkPhuongThucBan = $_POST['chkPhuongThucBan'];
		$t_chitiet = addslashes($_POST['t_chitiet']);
		$t_noibat = $_POST['t_noibat'];
		$t_khuyenmai = $_POST['t_khuyenmai'];
		$t_sapramat = $_POST['t_sapramat'];
		
		$update = "UPDATE ".$prefix."sanpham SET `t_loaisanpham` = '".$t_loaisanpham."', `t_nguoidang` = '".$t_nguoidang."', `t_tieude` = '".$t_tieude."', `t_hinhanh` = $DuongDanAnh, `t_mota` = '".$t_mota."', `t_giaban` = '".$t_giaban."', `t_giathitruong` = '".$t_giathitruong."', `t_loaitien` = '".$t_loaitien."', `t_loaitien_giathitruong` = '".$t_loaitien_giathitruong."', `t_quocgia` = '".$txtQuocGia."', `t_tinhthanh` = '".$txtTinhThanh."', `t_chitiet` = '".$t_chitiet."', `t_phuongthucban` = '".$chkPhuongThucBan."', `t_noibat` = '".$t_noibat."', `t_khuyenmai` = '".$t_khuyenmai."', `t_sapramat` = '".$t_sapramat."' WHERE t_id = '".$pid."'";
		
		$db->query($update);
		//Lay Id
		header("Location: ".$root."/chi-tiet-san-pham/".$pid."");
	}
?>
<div id="linkPath">
 
 <script language="javascript" type="text/javascript">
    function CheckVND()
    {
        if(document.getElementById("chkVND").checked==true)
        {
            document.getElementById("btnSave").style.display='';
            document.getElementById("btnSave_off").style.display='none';
        }
        else
        {
            document.getElementById("btnSave").style.display='none';
            document.getElementById("btnSave_off").style.display='';
        }
    }
</script>
       
<?
	$rs = $db->query("SELECT * FROM ".$prefix."sanpham WHERE t_id = '".$pid."' AND t_hienthi = 1");	
	if($db->num_rows($rs) != 0){
		$row = $db->fetch_array($rs);
		
		$t_loaisanpham = $row[t_loaisanpham];
		$t_nguoidang = $row[t_nguoidang];	
		//
		if($t_nguoidang != $_SESSION['uid']){reHome($urlHome);}
		//	
		$t_tieude = $myString->unQuote(stripslashes($row[t_tieude]));
		$t_hinhanh = $PathThumbFile . $row[t_hinhanh];
		$t_mota = $myString->unQuote(stripslashes($row[t_mota]));
		$t_giaban = $row[t_giaban];
		$t_loaitien = $row[t_loaitien];
		$t_giathitruong = $row[t_giathitruong];
		$t_loaitien_giathitruong = $row[t_loaitien_giathitruong];
		$t_quocgia = $row[t_quocgia];
		$t_tinhthanh = $row[t_tinhthanh];
		$t_ngaydang = format_date($row[t_ngaydang]);
		$t_chitiet = $myString->unQuote(stripslashes($row[t_chitiet]));
		$t_phuongthucban = $row[t_phuongthucban];
		$t_solanxem = $row[t_solanxem];
		$t_noibat = $row[t_noibat];
		$t_khuyenmai = $row[t_khuyenmai];
		$t_sapramat = $row[t_sapramat];
		$t_xemgannhat = $row[t_xemgannhat];
		$t_hienthi = $row[t_hienthi];
	}else{
		reHome($urlHome);
	}
	
?>

<div class="linkPath">
    <div class="corner-round-01-top-right">
        <div class="corner-round-01-top-left">
            <div class="corner-round-01-top-bg"></div>
        </div>

    </div>
    <div class="corner-round-01-body">    
	
        <?
			file_exist('includes/inc_thong_tin_rieng.php');
			include('includes/inc_thong_tin_rieng.php');	
		?>
		
        <div style="clear: both;"></div>
    </div>
    <div class="corner-round-01-bottom-right">

        <div class="corner-round-01-bottom-left">
            <div class="corner-round-01-bottom-bg"></div>
        </div>
    </div>   
</div>    
    </div>

<form action="" name="frmDangBan" id="frmDangBan" method="post" enctype="multipart/form-data" onsubmit="return funDangBanChecker();">
	<input type="hidden" name="pid" id="pid" value="<?=$pid?>" />
    <!-- Main Page-Content { -->    
    <div id="page-content">                            
        
<div id="product-new">
    <div class="corner-round-02-top-right">

        <div class="corner-round-02-top-left">
            <div class="corner-round-02-top-bg"></div>
        </div>
    </div>
    <div class="corner-round-02-body" style="height:auto;overflow:hidden">            
        <div id="error" class="form-error-group" style="none: block;width:816px">            
        </div>
        <div class="product-new-inner">            
            <div class="product-top-right"><div class="product-top-left"><div class="product-top-bg"></div></div></div>
            <div class="product-body">                    
                1. Tên sản phẩm <span style="font-size:8pt;color:Gray;font-weight:normal;">(<em>Vui lòng dùng tiếng Việt có dấu hoặc tiếng Anh, độ dài 40 ký tự</em>)</span>

            </div>
            <div class="product-bottom-right"><div class="product-bottom-left"><div class="product-bottom-bg"></div></div></div>
            <div class="product-new-control" style="margin-left:0px;text-align:center;margin-bottom:5px;">
                <input class="product-input-text" type="text" style="padding:3px;" id="t_tieude" name="t_tieude" value="<?=$t_tieude?>" />
            </div>
            <div style="margin-left:0px;text-align:left;margin-bottom:5px;margin-left:80px;margin-top:10px;font-size:10pt;">                
                <span class="notes" style="color:#666666;font-size:8pt;">
                    Mô tả ngắn :
                </span>
            </div>

            <div style="text-align:left;font-size:10pt;margin-bottom:10px;margin-left:80px;">  
                <input class="product-input-text2" type="text" id="t_mota" name="t_mota" value="<?=$t_mota?>"/>
            </div>      
            <div class="product-top-right"><div class="product-top-left"><div class="product-top-bg"></div></div></div>
            <div class="product-body">                    
                2. Chọn ngành hàng
            </div>
            <div class="product-bottom-right"><div class="product-bottom-left"><div class="product-bottom-bg"></div></div></div>
            <div class="product-new-control" style="margin-left:0px;text-align:center">                            
                <select style="margin:0" class="product-input-select" name="t_loaisanpham" id="t_loaisanpham">           
                    <option value=""></option>
				<?
					$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_hienthi = 1 AND c_id_cha = 0");
					while($row = $db->fetch_array($rs)){
					$c_ten = $myString->unQuote(stripslashes($row[c_ten]));
					$c_id = $row[c_id];
				?>	
                    <optgroup label="<?=$c_ten?>">
					<?
						$rs2 = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_hienthi = 1 AND c_id_cha = '".$c_id."'");
						while($row2 = $db->fetch_array($rs2)){
						$c_id2 = $row2[c_id];
						$c_ten2 = $row2[c_ten];
					?>
						<option value="<?=$c_id2?>" <? if($t_loaisanpham == $c_id2){ ?> selected="selected" <? }?>><?=$c_ten2?></option>
					<? } ?>
					</optgroup>
				<? } ?>
                </select>
            </div>
            
            <div class="product-top-right"><div class="product-top-left"><div class="product-top-bg"></div></div></div>
            <div class="product-body">                    
                3. Mô tả sản phẩm 
            </div>
            <div class="product-bottom-right"><div class="product-bottom-left"><div class="product-bottom-bg"></div></div></div>
            <div class="product-new-control">            
                <div style="margin-left:50px;"> 
					<p>
						<label class="title">Hàng khuyến mại</label>
						<span>
						<input type="radio" name="t_khuyenmai" value="0" <? if($t_khuyenmai == 0){?> checked="checked" <? } ?>/> Không
						<input type="radio" name="t_khuyenmai" value="1" <? if($t_khuyenmai == 1){?> checked="checked" <? } ?>/> Có
						</span>
					</p>
					<p>
						<label class="title">Hàng nổi bật</label>
						<span>
						<input type="radio" name="t_noibat" value="0" <? if($t_noibat == 0){?> checked="checked" <? } ?>/> Không
						<input type="radio" name="t_noibat" value="1" <? if($t_noibat == 1){?> checked="checked" <? } ?>/> Có
						</span>
					</p> 
					<p>
						<label class="title">Sắp ra mắt</label>
						<span>
						<input type="radio" name="t_sapramat" value="0" <? if($t_sapramat == 0){?> checked="checked" <? } ?>/> Không
						<input type="radio" name="t_sapramat" value="1" <? if($t_sapramat == 1){?> checked="checked" <? } ?>/> Có
						</span>
					</p> 
					<p>
                        <label class="title">Hình sản phẩm</label><span><img src="<?=$root?>/<?=$t_hinhanh?>" width="120" border="0" onerror="this.src='<?=$root?>/themes/images/noimg.jpg';"/><a href="?mod=capnhatsp&pid=<?=$pid?>&act=xoa" style="text-decoration:none; font-weight:bold;"> [ Xóa hình ] </a></span>
                    </p>                   
                    <p>
                        <label class="title">Thay hình mới</label>

                        <span id="fileDiv">
                            <input name="t_hinhanh" id="t_hinhanh" type="file" class="file" accept="image/gif, image/jpeg, image/jpg" size="73" /><br />                            
                        </span>
                    </p>
                    <p>

                        <label class="title">&nbsp;</label>
                        <span class="notes" style="color:#666666;font-size:0.8em;">
                            Hỗ trợ file hình định dạng *.jpg, *.gif, *.png. Dung lượng file hình tối đa 1Mb.
                        </span>
                        <br class="clearBoth"/> 
                    </p>                    
                    <p>
                        <label class="title">Giá bán</label>
						<input type="text" name="t_giaban" id="t_giaban" class="product-input-text txt-ie-fixed" style="float:left;width:145px;" value="<?=$t_giaban?>"/>
						<select name="t_loaitien" id="t_loaitien" class="product-input-select2" style="width:79px;margin-left:2px;">
						<?
							for($i=0;$i<count($arrLoaiTien);$i++){
						?>
						<option value="<?=$i?>" <? if($t_loaitien == $arrLoaiTien[$i]){ ?> selected="selected" <? }?>> <?=$arrLoaiTien[$i]?></option>
						<? } ?>
						</select>

                    </p>
					
					<p>
                        <label class="title">Giá thị trường</label>
						<input type="text" name="t_giathitruong" id="t_giathitruong" class="product-input-text txt-ie-fixed" style="float:left;width:145px;" value="<?=$t_giathitruong?>"/>
						<select name="t_loaitien_giathitruong" id="t_loaitien_giathitruong" class="product-input-select2" style="width:79px;margin-left:2px;">
						<?
							for($i=0;$i<count($arrLoaiTien);$i++){
						?>
						<option value="<?=$i?>" <? if($t_loaitien_giathitruong == $arrLoaiTien[$i]){ ?> selected="selected" <? }?>> <?=$arrLoaiTien[$i]?></option>
						<? } ?>
						</select>

                    </p>
					
                    <p>
                        <label class="title">Quốc gia</label>
						
					<select id="txtQuocGia" name="txtQuocGia" style="width:100px;" onchange="ajaxSelect2Select('txtQuocGia','txtTinhThanh','<?=$root?>/loadTinhThanh.php','IdQuocGia');"> 
					<option value="-1">---Chọn---</option>
					<? 
						$rs = $db->query("SELECT * FROM ".$prefix."quocgia order by qg_id ASC");
						while($row = $db->fetch_array($rs)){
					?>
						<option value="<?=$row[qg_id]?>"><?=$row[qg_ten]?></option>
					<? } ?>
					</select>
					<script language="javascript">
					  var a = document.getElementById('txtQuocGia');
					 for(var i=0;i<a.length;i++)
					 {if(a[i].value=="<?=$t_quocgia?>")
					 {a.selectedIndex=i;break;}}
				  </script>
					<label class="labelLine">Tỉnh/TP</label>
			        <select id="txtTinhThanh" name="txtTinhThanh">
						<option value="-1">---Chọn---</option>

						  <?
						  if($IdQuocGia == ""){$IdQuocGia = $t_quocgia;}
						  if($IdQuocGia) 
			
						  {
			
							$sql="select * from ".$prefix."tinhthanh where tt_maquocgia = '".$IdQuocGia."' order by tt_id ASC";
							$rs = $db->query($sql);
			
							if($db->num_rows($rs))
			
							{
			
								while($rows=$db->fetch_array($rs))
			
								{
			
								?>
			
								<option value="<?=$rows["tt_id"]?>"><?=$rows["tt_ten"]?></option>
			
								<?
			
								}
			
							}
			
						}
			
						?>

					</select>
              <script language="javascript">
			  	 var a = document.getElementById('txtTinhThanh');
				 for(var i=0;i<a.length;i++)
				 {if(a[i].value=="<?=$t_tinhthanh?>")
				 {a.selectedIndex=i;break;}}
	  		  </script>
                    </p>
                    <div>
                        <label class="title" id="lblSellingMethodID">Cách bán hàng</label>
                        <ul style="float:left;width:550px">
						<?
							for($i=0;$i<count($arrPhuongThucBanHang);$i++){
						?>
                            <li><input type="radio" name="chkPhuongThucBan" value="<?=$i?>" id="chkPhuongThucBan" <? if($i == $t_phuongthucban){?> checked="checked" <? }?> /><label><?=$arrPhuongThucBanHang[$i]?></label></li>
						<? } ?>	
							
                        </ul>

                    </div>                    
                </div>                
            </div>  
            <br class="clearBoth"/>  
            <div style="height:15px;font-size:0px;"></div>      
            <div class="product-top-right"><div class="product-top-left"><div class="product-top-bg"></div></div></div>
            <div class="product-body">                    
                4. Chi tiết sản phẩm <span style="font-size:8pt;color:Gray;font-weight:normal;">(<em>Vui lòng dùng tiếng Việt có dấu hoặc tiếng Anh</em>)</span>
            </div>
            <div class="product-bottom-right"><div class="product-bottom-left"><div class="product-bottom-bg"></div></div></div>        
            <div class="product-new-control" style="margin-left:80px;">

                <textarea style="width:80%; height:400px; margin:0 auto;" id="elm1" name="t_chitiet"><?=$t_chitiet?></textarea>
            </div>
                         
        </div>
        <div class="clearBoth" style="text-align: center;">
            <input name="btnPreview" onclick="javascript: confirmDelete('<?=$root?>/action.php?act=del_sanpham&u_id=<?=$t_nguoidang?>&id=<?=$pid?>');" id="btnPreview" src="<?=$root?>/themes/images/home/btnXoa.jpg" value="Xem trước" type="image">
            <input name="btnSave" onclick="javascript: return submitForm();" id="btnSave" src="<?=$root?>/themes/images/home/btnDangban.jpg" value="Đăng bán" type="image">
        </div>   
        <div style="color: rgb(136, 136, 136); font-size: 10pt;">

            (*) Nên và không nên khi đăng bán, xem <a target="_blank" href="javascript: void(0);">tại đây</a>
        </div>    

    </div>
    <div class="corner-round-02-bottom-right">
        <div class="corner-round-02-bottom-left">

            <div class="corner-round-02-bottom-bg"></div>
        </div>
    </div>   
</div>
<div style="height:10px"> </div>
<div id="uploadDiv"></div>       
    </div>
    <div style="clear:both;"></div>      
    <!-- } Main Page-Content -->  
	<input type="hidden" name="submit" id="submit" value="submit" />
</form>
                
            </div>
